package com.greatwall.hip.cms.controller;

import cn.hutool.core.util.NumberUtil;
import com.greatwall.hip.cms.entity.LyFkybqk;
import com.greatwall.hip.cms.entity.MedicalDiseaseData;
import com.greatwall.hip.cms.entity.MedicalServiceData;
import com.greatwall.hip.cms.model.Req;
import com.greatwall.hip.cms.model.Result;
import com.greatwall.hip.cms.service.LyFkybqkService;
import com.greatwall.hip.cms.service.MedicalDiseaseDataService;
import com.greatwall.hip.cms.service.MedicalServiceDataService;
import com.greatwall.hip.cms.vo.MedicalDiseaseAnalyseVO;
import com.greatwall.hip.cms.vo.OutpatientFrDistributedVO;
import com.greatwall.hip.cms.vo.OutpatientFxDistributedVO;
import com.greatwall.hip.cms.vo.extend.OutpatientFrDistributedExtendVO;
import com.greatwall.hip.cms.vo.extend.OutpatientFxDistributedExtendVO;
import com.greatwall.hip.cms.vo.extend.Top10DeptForInpatientVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (MedicalDiseaseData)表控制层
 *
 * @author makejava
 * @since 2021-07-06 14:59:39
 */
@RestController
@RequestMapping("medicalDiseaseData")
@Api(tags = "基本医疗模块", value = "医疗质量与安全&院感")
public class MedicalDiseaseDataController extends BaseController{
    /**
     * 服务对象
     */
    @Resource
    private MedicalDiseaseDataService medicalDiseaseDataService;

    @Resource
    private MedicalServiceDataService medicalServiceDataService;

    @Resource
    private LyFkybqkService lyFkybqkService;

    @ApiOperation("查询疾病统计表")
    @PostMapping("/analyseDisease")
    public Result<MedicalDiseaseAnalyseVO> analyseDragIncome(@RequestBody Req req) {
        checkDateReq(req);
        MedicalDiseaseData medicalDiseaseData = medicalDiseaseDataService.queryStatisticsData(req);
        MedicalServiceData medicalServiceData = medicalServiceDataService.queryStatisticsData(req);
        if( null == medicalDiseaseData) {
            medicalDiseaseData = new MedicalDiseaseData();
        }
        if( null == medicalServiceData) {
            medicalServiceData = new MedicalServiceData();
        }
        MedicalDiseaseAnalyseVO vo = new MedicalDiseaseAnalyseVO();
        vo.setFrCnt(medicalDiseaseData.getFrCnt());
        vo.setFxCnt(medicalDiseaseData.getFxCnt());
        vo.setCrPercent(NumberUtil.parseDouble(divScale(medicalDiseaseData.getCrbCnt(), medicalServiceData.getOutpatientPerson(), 2)));
        vo.setCtyxPercent(NumberUtil.parseDouble(divScale(medicalDiseaseData.getCtYxCnt(), medicalDiseaseData.getCtCnt(),2)));
        vo.setRyCySamePercent(NumberUtil.parseDouble(divScale(medicalDiseaseData.getRyCySameCnt(), medicalServiceData.getLeaveHospitalPerson(),2)));
        return Result.success(vo);
    }

    @ApiOperation("查询住院人数前十科室分布表")
    @PostMapping("/queryTop10DeptForInpatient")
    public Result<Top10DeptForInpatientVO> queryTop10DeptForInpatient(@RequestBody Req req) {
        checkAllReq(req);
        List<LyFkybqk> datas = lyFkybqkService.queryTop10DeptForInpatient(req.getStartDate(), req.getEndDate(),req.getType());
        Top10DeptForInpatientVO vo = new Top10DeptForInpatientVO();
        List<String> deptName = new ArrayList<>();
        List<Integer> persons = new ArrayList<>();
        for (int i = 0; i < datas.size(); i++) {
            deptName.add(datas.get(i).getDept());
            persons.add(datas.get(i).getPatientCount());
        }
        vo.setDeptName(deptName);
        vo.setPersons(persons);
        return Result.success(vo);
    }

    @ApiOperation("查询发热人次分布表")
    @PostMapping("/frDistributed")
    public Result<OutpatientFrDistributedExtendVO> frDistributed(@RequestBody Req req) {
        checkAllReq(req);
        List<String> date = rangeDate(req);
        OutpatientFrDistributedExtendVO vo = new OutpatientFrDistributedExtendVO();
        vo.setDate(date);
        List<Integer> frCnt = new ArrayList<>();
        List<OutpatientFrDistributedVO> datas = medicalDiseaseDataService.outpatientFrDistributed(req);
        for (int i = 0; i < date.size(); i++) {
            if(datas == null || datas.isEmpty()){
                frCnt.add(0);
            } else {
                int frCntData = 0;
                for (OutpatientFrDistributedVO outpatientFrDistributedVO : datas) {
                    if(StringUtils.equals(date.get(i), outpatientFrDistributedVO.getDatePoint())) {
                        frCntData = outpatientFrDistributedVO.getFrCnt();
                    }
                }
                frCnt.add(frCntData);
            }
        }
        vo.setFrCnt(frCnt);
        return Result.success(vo);
    }

    @ApiOperation("查询腹泻人次分布表")
    @PostMapping("/fxDistributed")
    public Result<OutpatientFxDistributedExtendVO> fxDistributed(@RequestBody Req req) {
        checkAllReq(req);
        List<String> date = rangeDate(req);
        OutpatientFxDistributedExtendVO vo = new OutpatientFxDistributedExtendVO();
        vo.setDate(date);
        List<Integer> fxCnt = new ArrayList<>();
        List<OutpatientFxDistributedVO> datas = medicalDiseaseDataService.outpatientFxDistributed(req);
        for (int i = 0; i < date.size(); i++) {
            if(datas == null || datas.isEmpty()){
                fxCnt.add(0);
            } else {
                int fxCntData = 0;
                for (OutpatientFxDistributedVO outpatientFxDistributedVO : datas) {
                    if (StringUtils.equals(date.get(i), outpatientFxDistributedVO.getDatePoint())) {
                        fxCntData = outpatientFxDistributedVO.getFxCnt();
                    }
                }
                fxCnt.add(fxCntData);
            }
        }
        vo.setFxCnt(fxCnt);
        return Result.success(vo);
    }
}
